package com.gudy.counter.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Random;

/*
* @param 生成验证码
* */
public class Captcha {
    /**
    * @param 验证码
    * */
    private String code;

    /**
    * @param 图片
    * */
    private BufferedImage buffImag;

    /**
    * @param 随机数发生器
    * */
    private Random random = new Random();

    /*
    * @param 生成随机的数字的字典
    * */
    private static final String RANDOM_CODE = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";
    /*
    * @param  width 宽度
    * @param  height 高度
    * @param codeCount 字符数量
    * @param lineCount 干扰线的数量
    * */
    public Captcha(int width,int height,int codeCount,int lineCount){
        //1.生成图像
        buffImag = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //2.给图像添加背景,200,250表示颜色的区间
        Graphics color = buffImag.getGraphics();
        color.setColor(getRandColoe(200,250));
        //填充颜色
        color.fillRect(0,0,width,height);
        //设置字体
        Font font = new Font("Fixedsys",Font.BOLD,height-5);
        color.setFont(font);
        //3.设置干扰线和噪点
        for (int i = 0 ; i < lineCount;i++){
            int xs = random.nextInt(width);
            int ys = random.nextInt(height);
            int xe = xs +random.nextInt(width);
            int ye = ys+random.nextInt(height);
            color.setColor(getRandColoe(1,255));
            color.drawLine(xs,ys,xe,ye);
        }
        //噪点
        float yawpRate = 0.01f;
        int area =(int)(yawpRate * width *height);
        for ( int i = 0 ; i < area ; i++ ){
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            buffImag.setRGB(x,y,random.nextInt(255));
        }
        //4.把字符添加到图片山上
        this.code = randomCode(codeCount);
        int fontWidth =  width /codeCount;
        int fontHeight = height-5;
        for (int i = 0 ; i < codeCount ; i++){
            String appenCode = this.code.substring(i,i+1);
            color.setColor(getRandColoe(1,255));
            color.drawString(appenCode,i*fontWidth+3,fontHeight-3);
        }
    }
    /**
     * @param  codeCount
     * @param 随机生成字符
    * */
    private String randomCode(int codeCount) {
        StringBuffer strBuff = new StringBuffer();
        int len = RANDOM_CODE.length() -1;
        double r ;
        for (int i = 0 ; i < codeCount;i++){
            r= (Math.random())*len;
            strBuff.append(RANDOM_CODE.charAt((int)r));

        }
        return strBuff.toString();
    }

    private Color getRandColoe(int colorStart, int colorEnd) {
       if (colorStart > 255){
           colorStart = 255;
       }
       if (colorEnd > 255){
           colorEnd = 255;
       }
       int r =  colorStart+random.nextInt(colorEnd-colorStart);
       int g =  colorStart+random.nextInt(colorEnd-colorStart);
       int b = colorStart+random.nextInt(colorEnd-colorStart);
       return new Color(r,g,b);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public BufferedImage getBuffImag() {
        return buffImag;
    }

    public void setBuffImag(BufferedImage buffImag) {
        this.buffImag = buffImag;
    }

    public String getBasee64ByteStr() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(buffImag,"png",baos);
        String base64Str = Base64.getEncoder().encodeToString(baos.toByteArray());
        base64Str = base64Str.replaceAll("\n","").replaceAll("\r","");
        return "data:image/jpg;base64,"+base64Str;
    }
}
